lib = Dir('lib')
build = Dir('build')
src = Dir('src')
inc = Dir('include')
bin = Dir('bin')

prefix = Dir(ARGUMENTS.get('prefix', '.'))
prefix_lib = prefix.Dir('lib')
prefix_inc = prefix.Dir('include')
prefix_bin = prefix.Dir('bin')


import os
cpath = os.environ.get('CPATH', "")
ld_library_path = os.environ.get('LD_LIBRARY_PATH', "")
cpp_paths = cpath.split(':')+[prefix_inc, inc]
lib_paths = ld_library_path.split(':')+[prefix_lib, lib]

ccflags = ['-Wall', '-Werror', '-ansi']
if 'debug' in COMMAND_LINE_TARGETS:
	ccflags += ['-g']

BuildDir(build, src, duplicate=0)

build_sources = [build.File('mazes.cpp'), build.File('flag_maze.cpp')]
glue_env_sources = [build.File('glue_maze_env.cpp')]
expert_maze_agent_sources = [build.File('expert_maze_agent.cpp')]
pddl_sources = [build.File('pddl.cpp')]
dia_sources = [build.File('dia.cpp')]
headers = [inc.File('crl/mazes.hpp')]

env = Environment(tools = ["default", "tools"], toolpath = '.',
                  CPPPATH=cpp_paths, CCFLAGS=ccflags, LIBPATH=lib_paths)

shared_lib_suffix = ".so"
if os.uname()[0] == "Darwin":
	shared_lib_suffix = ".dylib"
	env.Append(SHLINKFLAGS='-undefined dynamic_lookup')

config = Configure(env)
config.CheckLib('z')
config.CheckLib('gzstream')
config.CheckCXXHeader('boost/shared_ptr.hpp')
config.CheckCXXHeader('boost/multi_array.hpp')
config.CheckLibWithHeader('strxml', 'strxml.hpp', 'cpp')
config.CheckLibWithHeader('crl', 'crl/crl.hpp', 'cpp')

has_diastream = config.CheckLibWithHeader('diastream', 'diastream.hpp', 'cpp')

has_glue = sum([config.CheckHeader('rlglue/Agent_common.h'),
                config.CheckHeader('rlglue/utils/C/RLStruct_util.h'),
                config.CheckHeader('rlglue/utils/C/TaskSpec_Parser.h'),
                config.CheckLib('rlutils'),
                config.CheckLib('rlagent'),
                config.CheckLib('rlgluenetdev')])
               
config.CheckLibWithHeader('rlcrlenv', 'crl/glue_env.hpp', 'cpp')
env = config.Finish()

lib_targets = []
bin_targets = []

libmazes = env.SharedLibrary(lib.File('libmazes'+shared_lib_suffix), build_sources,
                             LIBS=['strxml', 'crl'])
lib_targets += [libmazes]

maze_pddl = env.Program(bin.Entry('maze_pddl'), pddl_sources,
                        LIBS=['mazes', 'strxml', 'crl'])
bin_targets += [maze_pddl]

if has_diastream:
	maze_dia = env.Program(bin.Entry('maze_dia'), dia_sources,
	                       LIBS=['mazes', 'strxml', 'crl',
	                             'diastream', 'gzstream', 'z'])
	bin_targets += [maze_dia]
else:
	print "Cannot build maze_dia"

if has_glue:
	glue_maze_env = env.Program(bin.Entry('glue_maze_env'), glue_env_sources,
	                            LIBS=['mazes', 'strxml', 'crl', 'rlgnmenv', 'rlcrlenv',
	                                  'rlutils', 'rlgluenetdev'])
	bin_targets += [glue_maze_env]

	expert_maze_agent = env.Program(bin.Entry('expert_maze_agent'),
	                                expert_maze_agent_sources,
	                                LIBS=['strxml', 'crl', 'rlgnmagent',
	                                      'rlcrlagent', 'rlutils', 'rlgluenetdev',
	                                      'mazes'])
	bin_targets += [expert_maze_agent]
else:
	print "Cannot build glue_maze_env or expert_maze_agent"

	
if prefix != Dir('.'):
	for header in headers:
		env.Alias('install', env.InstallPerm(prefix_inc.Dir('crl'), inc.Entry(header), 0644))
	env.Alias('install', env.InstallPerm(prefix_lib, lib_targets, 0644))
	env.Alias('install', env.InstallPerm(prefix_bin, bin_targets, 0755))

for target in lib_targets+bin_targets:
	Default(target)
	env.Alias('debug', target)

dist_files = [File(x) for x in """
COPYING COPYING.LESSER SConstruct tools.py
cfgs/slip01.xml data/aaai.xml
""".split()]
dist_files += [inc.Entry(header) for header in headers]

dist_files += [src.Entry(build_source) for build_source in build_sources] 
dist_files += [src.Entry(glue_env_source) for glue_env_source in glue_env_sources] 
dist_files += [src.Entry(expert_maze_agent_source) for expert_maze_agent_source in expert_maze_agent_sources] 
dist_files += [src.Entry(pddl_source) for pddl_source in pddl_sources] 
dist_files += [src.Entry(dia_source) for dia_source in dia_sources]

env.Alias('dist', env.DistTar('mazes.tar.gz', dist_files))
